package com.example.xiaodong.cn.dajiqi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 董豪华 on 17-6-20.
 */

public class LoginActivity extends AppCompatActivity {
    //1ed60d5e27951
    private RequestQueue mQueue;
    private EditText input_name;
    private EditText input_pwd ;
    private Button bu_login;
    private LinearLayout zhu_ll;
    private CheckBox cb_jizhu;
    private SharedPreferences mPreferences;
    private Button bu_zhuce;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mQueue  = Volley.newRequestQueue(LoginActivity.this);

        inView();

    }

    /**
     * 初始化控件
     */
    private void inView() {
        input_name = (EditText) findViewById(R.id.input_name);
        input_pwd = (EditText) findViewById(R.id.input_pwd);
        bu_login = (Button) findViewById(R.id.bu_login);
        zhu_ll = (LinearLayout) findViewById(R.id.zhu_ll);
        cb_jizhu = (CheckBox) findViewById(R.id.cb_jizhu);
        bu_zhuce = (Button) findViewById(R.id.bu_zhuce);
        Intent intent = getIntent();
        String name1 = intent.getStringExtra("name");
        String pwd1 = intent.getStringExtra("pwd");
        input_name.setText(name1);
        input_pwd.setText(pwd1);
        mPreferences = getSharedPreferences("user",MODE_PRIVATE);
        String name = mPreferences.getString("name", "");
        String pwd = mPreferences.getString("pwd", "");
        if (!name.equals("") && !pwd.equals("")){
            login(name,pwd);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        bu_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = input_name.getText().toString();
               String pwd = input_pwd.getText().toString();
                if (name.equals("")){
                    prompt("用户名不能为空哦");
                }else if (pwd.equals("")){
                    prompt("密码不能为空");
                }else {
                    if (cb_jizhu.isChecked()){
                        SharedPreferences.Editor edit = mPreferences.edit();
                        edit.putString("name",name);
                        edit.putString("pwd",pwd);
                        edit.commit();
                        login(name,pwd);
                    }else {
                        login(name,pwd);
                    }
                }

            }
        });
        bu_zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegiActivity.class);
                startActivity(intent);

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    /**
     * 用于登录
     * @param name 用户名
     * @param pwd 密码
     */
    private void login(String name ,String pwd){
        //http://apicloud.mob.com/user/login?key=123456&username=tangsir&password=4AC36A18EA02AC6C
        String url = "http://apicloud.mob.com/user/login?key=1ed60d5e27951&username="+name+"&password="+pwd;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    String retCode = object.optString("retCode");
                    String msg = object.optString("msg");
                    if (retCode.equals("22807")){
                        prompt(msg);
                    }else {
                        JSONObject result = object.optJSONObject("result");
                        String token = result.optString("token");
                        String uid = result.optString("uid");
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    prompt(e.toString());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                prompt(error.toString());
            }
        });
        mQueue.add(request);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue = null;
    }

    /**
     * 提示
     * @param str
     * 要提示的信息
     */
    private void prompt(String str){
        Snackbar.make(zhu_ll, str,Snackbar.LENGTH_LONG).show();
    }
}
